import API from './api'
import request from '../utils/request'

// 添加销售数据
export const addOnSalesData = ({...allData} = {}) => request({
  url: API.ONSALES_API,
  method: 'POST',
  data: {
    ...allData,
  },
})

// 根据商品 id 查询已出售
export const getOnSaleDataOfGoodsId = (stockId) => request({
  url: API.ONSALES_API,
  method: 'GET',
  params: {
    stockId,
  },
})

// 删除销售数据
export const deleteOnSalesData = (id) => request({
  url: `${API.ONSALES_API}/${id}`,
  method: 'DELETE',
})

// 获取所有正在销售商品的总数
export const getOnSalesLength = ({...options} = {}) => request({
  url: API.ONSALES_API,
  method: 'GET',
  params: {
    ...options,
  },
}).then((data) => data.length)
  .catch((err) => {
    console.log(err)
  })

// 查询销售商品数据终极版
export const getOnSalesUltimate = ({_page = 1, _limit = 3, ...otherOptions} = {}) => request({
  url: API.ONSALES_API,
  method: 'GET',
  params: {
    _page,
    _limit,
    ...otherOptions,
  },
})

// 修改指定商品的折扣
export const updateOnSaleDiscount = (id, discount) => request({
  url: API.ONSALES_API + '/' + id,
  method: 'PATCH',
  data: {
    discount,
  },
})

// 修改指定商品的折扣
export const updateOnSaleAmountOrPrice = (id, {...options}) => request({
  url: API.ONSALES_API + '/' + id,
  method: 'PATCH',
  data: {
    ...options,
  },
})

// 查询阈值以下的在售商品
export const getOnSalesTipsData = (amount_lte) => request({
  url: API.ONSALES_API,
  method: 'GET',
  params: {
    amount_lte,
  },
})


// 获取已销售的数据
// 查询销售商品数据终极版
export const getHasSalesUltimate = ({_page = 1, _limit = 3, ...otherOptions} = {}) => request({
  url: API.HASSALES_API,
  method: 'GET',
  params: {
    _page,
    _limit,
    ...otherOptions,
  },
})
